%----------------------------------
% 图形相关的项
% 需要 TexLive 2020 版本

% 自定义填充格式，用于实现绘制阴影
% TikZ 提供的 north east lines 等默认 pattern, 或 Lines ，有个问题：使用不同的PDF阅读器，会得到不同的效果。
% 如：
%       VSCode 中 LaTeX WorkShop 看到的是线段，
%       SumatraPDF 看到的是连贯的线条。
% 所以这里采用自定义的格式。
% 下面的代码基本是将 Lines 的代码修改而成。
% Lines实现中，bounding box 是 (-0.5\distance, -0.5\distance) 到 (0.5\distance, 0.5\distance)
% 这里的 TikZ 实现是 (0, 0) 到 (\distance, \distance)，按说应该没有区别，但实际效果确有不同。
\pgfdeclarepattern{
  name=mylines,
  parameters={
      \pgfkeysvalueof{/pgf/pattern keys/distance},
      \pgfkeysvalueof{/pgf/pattern keys/angle},
      \pgfkeysvalueof{/pgf/pattern keys/xshift},
      \pgfkeysvalueof{/pgf/pattern keys/yshift},
      \pgfkeysvalueof{/pgf/pattern keys/line width},
  },
  bottom left={%
    \pgfpoint
      {0}%
      {0}},
  top right={%
    \pgfpoint
      {(\pgfkeysvalueof{/pgf/pattern keys/distance})}%
      {(\pgfkeysvalueof{/pgf/pattern keys/distance})}},
  tile size={%
    \pgfpoint
      {\pgfkeysvalueof{/pgf/pattern keys/distance}}%
      {\pgfkeysvalueof{/pgf/pattern keys/distance}}},
  tile transformation={%
    \pgftransformshift{%
      \pgfpoint
        {\pgfkeysvalueof{/pgf/pattern keys/xshift}}%
        {\pgfkeysvalueof{/pgf/pattern keys/yshift}}}%
    \pgftransformrotate{\pgfkeysvalueof{/pgf/pattern keys/angle}}},
  defaults={
    distance/.initial=3pt,
    angle/.initial=0,
    xshift/.initial=0pt,
    yshift/.initial=0pt,
    line width/.initial=\the\pgflinewidth,
  },
  code={%
    \pgfsetlinewidth{\pgfkeysvalueof{/pgf/pattern keys/line width}}%
    \pgfpathmoveto{\pgfpoint{0pt}{0pt}}%
    \pgfpathlineto{\pgfpoint{(\pgfkeysvalueof{/pgf/pattern keys/distance})}{0pt}}%
    \pgfusepath{stroke}%
  },
}



% 绘制 “原子结构示意图”
% 共有三个参数：
%   1. number : 核电荷数
%   2. layer  : 各电子层电子数。如 2,8,1
%   3. direction : 电子层显示位置。 可选值：left / right
%
% 调用示例：
%   \draw (0, 0) pic{atom};
%   \draw (3, 0) pic{atom={number=2}};
%   \draw (6, 0) pic{atom={number=5, layer={1,2,2}}};
%   \draw (11, 0) pic{atom={number=5, direction=left}};
\tikzset{
  atom/.pic={
    \begin{scope}[atom options/.cd,#1]
      \pgfkeysgetvalue{/tikz/atom options/number}{\atomnumber}
      \pgfkeysgetvalue{/tikz/atom options/layer}{\atomlayer}
      \if \atomlayer 0
        \pgfkeysgetvalue{/tikz/atom values/\atomnumber}{\loopcontent}
      \else
        \pgfkeysgetvalue{/tikz/atom options/layer}{\loopcontent}
      \fi

      \draw (0, 0) circle (0.5) node {$+ \atomnumber$};
      \foreach \n  [count=\i] in  \loopcontent {
        \pgfmathsetmacro{\r}{0.5 + \i/2}
        \draw (0, 0) + (\atomstartangle:\r) arc (\atomstartangle:\atomstartangle+80:\r) node [midway, fill=white, inner sep=2pt] {$\n$};
      }
    \end{scope}
  },
  atom options/.is family,
  atom options/.cd,
    number/.initial = 1,  % 核电荷数.
    layer/.initial  = 0,  % 缺省值0，表示通过 "核电荷数" 来获取对应的电子层信息。
    direction/.is choice,
      direction/right/.code = {\def\atomstartangle{-40}},
      direction/left/.code  = {\def\atomstartangle{140}},
      direction=right,
  /tikz/atom values/.cd, % 数据来自：https://wenku.baidu.com/view/51fdf0caae1ffc4ffe4733687e21af45b207fe36
    1/.initial  = {1},  % 氢 H
    2/.initial  = {2},  % 氦 He
    3/.initial  = {2, 1},  % 锂 Li
    4/.initial  = {2, 2},  % 铍 Be
    5/.initial  = {2, 3},  % 硼 B
    6/.initial  = {2, 4},  % 碳 C
    7/.initial  = {2, 5},  % 氮 N
    8/.initial  = {2, 6},  % 氧 O
    9/.initial  = {2, 7},  % 氟 F
    10/.initial = {2, 8},  % 氖 Ne
    11/.initial = {2, 8,  1},  % 钠 Na
    12/.initial = {2, 8,  2},  % 镁 Mg
    13/.initial = {2, 8,  3},  % 铝 Al
    14/.initial = {2, 8,  4},  % 硅 Si
    15/.initial = {2, 8,  5},  % 磷 P
    16/.initial = {2, 8,  6},  % 硫 S
    17/.initial = {2, 8,  7},  % 氯 Cl
    18/.initial = {2, 8,  8},  % 氩 Ar
    19/.initial = {2, 8,  8, 1},  % 钾 K
    20/.initial = {2, 8,  8, 2},  % 钙 Ca
    21/.initial = {2, 8,  9, 2},  % 钪 Sc
    22/.initial = {2, 8, 10, 2},  % 钛 Ti
    23/.initial = {2, 8, 11, 2},  % 钒 V
    24/.initial = {2, 8, 13, 1},  % 铬 Cr
    25/.initial = {2, 8, 13, 2},  % 锰 Mn
    26/.initial = {2, 8, 14, 2},  % 铁 Fe
    27/.initial = {2, 8, 15, 2},  % 钴 Co
    28/.initial = {2, 8, 16, 2},  % 镍 Ni
    29/.initial = {2, 8, 18, 1},  % 铜 Cu
    30/.initial = {2, 8, 18, 2},  % 锌 Zn
    31/.initial = {2, 8, 18, 3},  % 镓 Ga
    32/.initial = {2, 8, 18, 4},  % 锗 Ge
    33/.initial = {2, 8, 18, 5},  % 砷 As
    34/.initial = {2, 8, 18, 6},  % 硒 Se
    35/.initial = {2, 8, 18, 7},  % 溴 Se
    36/.initial = {2, 8, 18, 8},  % 氪 Kr
    37/.initial = {2, 8, 18,  8, 1},  % 铷 Rb
    38/.initial = {2, 8, 18,  8, 2},  % 锶 Sr
    39/.initial = {2, 8, 18,  9, 2},  % 钇 Y
    40/.initial = {2, 8, 18, 10, 2},  % 锆 Zr
    41/.initial = {2, 8, 18, 12, 1},  % 铌 Nb
    42/.initial = {2, 8, 18, 13, 1},  % 钼 Mo
    43/.initial = {2, 8, 18, 13, 2},  % 锝 Tc
    44/.initial = {2, 8, 18, 15, 1},  % 钌 Ru
    45/.initial = {2, 8, 18, 16, 1},  % 铑 Rh
    46/.initial = {2, 8, 18, 18},     % 钯 Pd 。 疑似有误
    47/.initial = {2, 8, 18, 18, 1},  % 银 Ag
    48/.initial = {2, 8, 18, 18, 2},  % 镉 Cd
    49/.initial = {2, 8, 18, 18, 3},  % 铟 In
    50/.initial = {2, 8, 18, 18, 4},  % 锡 Sn
    51/.initial = {2, 8, 18, 18, 5},  % 锑 Sb
    52/.initial = {2, 8, 18, 18, 6},  % 碲 Te
    53/.initial = {2, 8, 18, 18, 7},  % 碘 I
    54/.initial = {2, 8, 18, 18, 8},  % 氙 Xe
    55/.initial = {2, 8, 18, 18,  8, 1},  % 铯 Cs
    56/.initial = {2, 8, 18, 18,  8, 2},  % 钡 Ba
    57/.initial = {2, 8, 18, 18,  9, 1},  % 镧 La
    58/.initial = {2, 8, 18, 19,  9, 2},  % 铈 Ce
    59/.initial = {2, 8, 18, 21,  8, 2},  % 镨 Pr
    60/.initial = {2, 8, 18, 22,  8, 2},  % 钕 Nd
    61/.initial = {2, 8, 18, 23,  8, 2},  % 钷 Pm
    62/.initial = {2, 8, 18, 24,  8, 2},  % 钐 Sm
    63/.initial = {2, 8, 18, 25,  8, 2},  % 铕 Eu
    64/.initial = {2, 8, 18, 25,  9, 2},  % 钆 Gd
    65/.initial = {2, 8, 18, 27,  8, 2},  % 铽 Td
    66/.initial = {2, 8, 18, 28,  8, 2},  % 镝 Dy
    67/.initial = {2, 8, 18, 29,  8, 2},  % 钬 Ho
    68/.initial = {2, 8, 18, 30,  8, 2},  % 铒 Er
    69/.initial = {2, 8, 18, 31,  8, 2},  % 铥 Tm
    70/.initial = {2, 8, 18, 32,  8, 2},  % 镱 Yb
    71/.initial = {2, 8, 18, 32,  9, 2},  % 镥 Lu
    72/.initial = {2, 8, 18, 32, 10, 2},  % 铪 Hf
    73/.initial = {2, 8, 18, 32, 11, 2},  % 钽 Ta
    74/.initial = {2, 8, 18, 32, 12, 2},  % 钨 W
    75/.initial = {2, 8, 18, 32, 13, 2},  % 铼 Re
    76/.initial = {2, 8, 18, 32, 14, 2},  % 锇 Os
    77/.initial = {2, 8, 18, 32, 15, 2},  % 铱 Ir
    78/.initial = {2, 8, 18, 32, 17, 1},  % 铂 Pt
    79/.initial = {2, 8, 18, 32, 18, 1},  % 金 Au
    80/.initial = {2, 8, 18, 32, 18, 2},  % 汞 Hg
    81/.initial = {2, 8, 18, 32, 18, 3},  % 铊 Tl
    82/.initial = {2, 8, 18, 32, 18, 4},  % 铅 Pb
    83/.initial = {2, 8, 18, 32, 18, 5},  % 铋 Bi
    84/.initial = {2, 8, 18, 32, 18, 6},  % 钋 Po
    85/.initial = {2, 8, 18, 32, 18, 7},  % 砹 At
    86/.initial = {2, 8, 18, 32, 18, 8},  % 氡 Rn
    87/.initial = {2, 8, 18, 32, 18,  8, 1},  % 钫 Fr
    88/.initial = {2, 8, 18, 32, 18,  8, 2},  % 镭 Ra
    89/.initial = {2, 8, 18, 32, 18,  9, 2},  % 锕 Ac
    90/.initial = {2, 8, 18, 32, 18, 10, 2},  % 钍 Th
    91/.initial = {2, 8, 18, 32, 20,  9, 2},  % 镤 Pa
    92/.initial = {2, 8, 18, 32, 21,  9, 2},  % 铀 U
    93/.initial = {2, 8, 18, 32, 22,  9, 2},  % 镎 Np
    94/.initial = {2, 8, 18, 32, 24,  8, 2},  % 钚 Pu
    95/.initial = {2, 8, 18, 32, 25,  8, 2},  % 镅 Am
    96/.initial = {2, 8, 18, 32, 25,  9, 2},  % 锔 Cm
    97/.initial = {2, 8, 18, 32, 27,  8, 2},  % 锫 Bk
    98/.initial = {2, 8, 18, 32, 28,  8, 2},  % 锎 Cf
    99/.initial = {2, 8, 18, 32, 29,  8, 2},  % 锿 Es
    100/.initial= {2, 8, 18, 32, 30,  8, 2},  % 镄 Fm
    101/.initial= {2, 8, 18, 32, 31,  8, 2},  % 钔 Md
    102/.initial= {2, 8, 18, 32, 32,  8, 2},  % 锘 No
    103/.initial= {2, 8, 18, 32, 32,  9, 2},  % 铹 Lr
    104/.initial= {2, 8, 18, 32, 32, 10, 2},  % 𬬻 Rf
    105/.initial= {2, 8, 18, 32, 32, 11, 2},  % 𬭊 Db
}

